package com.xxx.blog.controller;


import com.xxx.blog.data.Cartdata;
import com.xxx.blog.entity.UserBook;
import com.xxx.blog.service.BooksService;
import com.xxx.blog.service.UserBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author WangXJ
 * @since 2021-08-18
 */
@RestController
@RequestMapping("/blog/user-book")
public class UserBookController {

    @Autowired
    UserBookService userBookService;
    @Autowired
    BooksService booksService;

    @RequestMapping("/insertOneInCar/{uid}/{bid}/{bsum}")
    public int insertOneInCar(@PathVariable("uid") int uid,
                              @PathVariable("bid") int bid,
                              @PathVariable("bsum") int bsum){
        return userBookService.insertOneInCar(uid,bid,bsum);
    }

    @RequestMapping("/findCartByUid/{uid}")
    public List<Cartdata> findCartByUid(@PathVariable("uid") int uid) {
        List<Cartdata> cartList = new ArrayList<>();
        List<UserBook> list = userBookService.findCartByUid(uid);
        list.forEach(item -> {
            Cartdata cart = new Cartdata();
            cart.setBooks(booksService.findBookById(item.getBid()));
            cart.setCartsum(item.getBsum());
            cartList.add(cart);
        });
        return cartList;
    }

    @RequestMapping("/deleteOne/{uid}/{bid}")
    public int deleteOneById(@PathVariable("uid") int uid,
                             @PathVariable("bid") int bid) {
        return userBookService.deleteOne(uid,bid);
    }
}

